package com.yunc.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunc.common.utils.R;
import com.yunc.modules.sys.entity.SysDept;
import com.yunc.modules.sys.mapper.SysDeptMapper;
import com.yunc.modules.sys.service.ISysDeptService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 部门
 * @author congcong tao
 * @create 2018/11/20 下午9:53
 */
@Service("sysDeptService")
public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper,SysDept> implements ISysDeptService {

    @Autowired
    private SysDeptMapper sysDeptMapper;

    @Override
    public List<SysDept> queryDeptList(SysDept sysDept) {
        QueryWrapper<SysDept> ew = new QueryWrapper<SysDept>().eq("parent_id", 0L);
        List<SysDept> deptList = super.list(ew);
        if (deptList != null && deptList.size() == 1) {
            deptList.get(0).setOpen(Boolean.TRUE);
        }
        return getTreeDept(deptList);
    }

    @Override
    public String selectMaxCode(Long parentId) {
        return sysDeptMapper.selectMaxCode(parentId);
    }

    @Override
    public R saveDept(SysDept dept) {
        dept.setCreateTime(new Date());
        // 查询code
        Long parentId = dept.getParentId();
        String code = this.selectMaxCode(dept.getParentId());
        if (StringUtils.isNotEmpty(code)) {
            int i = Integer.parseInt(code) + 1;
            dept.setCode(String.valueOf(i));
        } else {
            // 取出父节点
            if (parentId != null) {
                SysDept parentDept = this.getById(parentId);
                dept.setCode(parentDept.getCode() + "01");
            } else{
                dept.setCode("10");
            }
        }
        boolean flag = this.save(dept);
        if (flag) {
            return R.ok();
        }
        return R.error();
    }

    @Override
    public R updateDept(SysDept dept) {
        SysDept currentDept = this.getById(dept.getId());
        Long parentId = dept.getParentId();
        if (!parentId.equals(currentDept.getParentId())) {
            String code = this.selectMaxCode(parentId);
            if (StringUtils.isNotEmpty(code)) {
                int i = Integer.parseInt(code) + 1;
                dept.setCode(String.valueOf(i));
            } else {
                // 取出父节点
                if (parentId != null) {
                    SysDept parentDept = this.getById(parentId);
                    dept.setCode(parentDept.getCode() + "01");
                }
            }
        }
        boolean flag = this.updateById(dept);
        if (flag) {
            return R.ok();
        }
        return R.error();
    }

    private List<SysDept> getTreeDept(List<SysDept> deptList) {
        List<SysDept> subDeptList = new ArrayList<>();
        for (SysDept sysDept : deptList) {
            QueryWrapper<SysDept> ew = new QueryWrapper<SysDept>()
                    .eq("parent_id", sysDept.getId())
                    .orderByAsc("order_num");
            List<SysDept> deptListC = super.list(ew);
            if (deptListC != null && deptListC.size() > 0) {
                sysDept.setChildren(getTreeDept(deptListC));
            } else {
                sysDept.setIsLast(Boolean.TRUE);
            }
            subDeptList.add(sysDept);
        }
        return subDeptList;
    }
}
